package Part3_类的加载器;

import Part1_反射调用私有结构.Person;
import org.junit.Test;

import java.io.*;
import java.util.Properties;

/**
 * ClassName: ClassLoaderTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.05.05 4:03
 */
public class ClassLoaderTest {

    /**
     * 在JDK8下执行
     */
    @Test
    public void test1(){
        //系统类加载器
        ClassLoader classLoader1 = ClassLoader.getSystemClassLoader();
        System.out.println(classLoader1);//sun.misc.Launcher$AppClassLoader

        //装载类加载器
        ClassLoader classLoader2 = classLoader1.getParent();
        System.out.println(classLoader2);//sun.misc.Launcher$ExtClassLoader

        //上层的引导类加载器获取失败：null (引导类加载器为C++编写)
        ClassLoader classLoader3 = classLoader2.getParent();
        System.out.println(classLoader3);//null
    }

    @Test
    public void test2() throws ClassNotFoundException {
        //用户自定义的类为系统加载器加载(AppClassLoader)
        Class<Person> class1 = Person.class;
        System.out.println(class1.getClassLoader());//sun.misc.Launcher$AppClassLoader


        //lang包所在的核心API使用引导类加载器加载
        Class class2 = Class.forName("java.lang.String");
        System.out.println(class2.getClassLoader());//null

    }

    //Properties:处理属性文件
    @Test
    public void test3() throws IOException {
        Properties pros = new Properties();

        FileInputStream fis = new FileInputStream(new File("info.properties"));

        pros.load(fis);
        String name = pros.getProperty("name");
        String age = pros.getProperty("age");

        System.out.println(name+"-->"+age);
    }

    @Test
    public void test4() throws IOException {
        Properties pros = new Properties();

        //通过类的加载器获取文件的路径为：当前module的src下
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("info.properties");

        pros.load(inputStream);
        String name = pros.getProperty("name");
        String age = pros.getProperty("age");

        System.out.println(name+"-->"+age);
    }
}
